package com.ruoyi.sports.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sports.domain.Event;
import com.ruoyi.sports.mapper.SportsMeetingMapper;
import com.ruoyi.sports.domain.SportsMeeting;
import com.ruoyi.sports.service.ISportsMeetingService;

/**
 * 运动会Service业务层处理
 * 
 * @author ZJH
 * @date 2023-03-08
 */
@Service
public class SportsMeetingServiceImpl implements ISportsMeetingService 
{
    @Autowired
    private SportsMeetingMapper sportsMeetingMapper;

    /**
     * 查询运动会
     * 
     * @param sportsMeetingId 运动会主键
     * @return 运动会
     */
    @Override
    public SportsMeeting selectSportsMeetingBySportsMeetingId(Long sportsMeetingId)
    {
        return sportsMeetingMapper.selectSportsMeetingBySportsMeetingId(sportsMeetingId);
    }

    /**
     * 查询运动会列表
     * 
     * @param sportsMeeting 运动会
     * @return 运动会
     */
    @Override
    public List<SportsMeeting> selectSportsMeetingList(SportsMeeting sportsMeeting)
    {
        return sportsMeetingMapper.selectSportsMeetingList(sportsMeeting);
    }

    /**
     * 新增运动会
     * 
     * @param sportsMeeting 运动会
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSportsMeeting(SportsMeeting sportsMeeting)
    {
        int rows = sportsMeetingMapper.insertSportsMeeting(sportsMeeting);
        insertEvent(sportsMeeting);
        return rows;
    }

    /**
     * 修改运动会
     * 
     * @param sportsMeeting 运动会
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSportsMeeting(SportsMeeting sportsMeeting)
    {
        sportsMeetingMapper.deleteEventBySportsMeetingId(sportsMeeting.getSportsMeetingId());
        insertEvent(sportsMeeting);
        return sportsMeetingMapper.updateSportsMeeting(sportsMeeting);
    }

    /**
     * 批量删除运动会
     * 
     * @param sportsMeetingIds 需要删除的运动会主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSportsMeetingBySportsMeetingIds(Long[] sportsMeetingIds)
    {
        sportsMeetingMapper.deleteEventBySportsMeetingIds(sportsMeetingIds);
        return sportsMeetingMapper.deleteSportsMeetingBySportsMeetingIds(sportsMeetingIds);
    }

    /**
     * 删除运动会信息
     * 
     * @param sportsMeetingId 运动会主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSportsMeetingBySportsMeetingId(Long sportsMeetingId)
    {
        sportsMeetingMapper.deleteEventBySportsMeetingId(sportsMeetingId);
        return sportsMeetingMapper.deleteSportsMeetingBySportsMeetingId(sportsMeetingId);
    }

    /**
     * 新增赛事项目信息
     * 
     * @param sportsMeeting 运动会对象
     */
    public void insertEvent(SportsMeeting sportsMeeting)
    {
        List<Event> eventList = sportsMeeting.getEventList();
        Long sportsMeetingId = sportsMeeting.getSportsMeetingId();
        if (StringUtils.isNotNull(eventList))
        {
            List<Event> list = new ArrayList<Event>();
            for (Event event : eventList)
            {
                event.setSportsMeetingId(sportsMeetingId);
                list.add(event);
            }
            if (list.size() > 0)
            {
                sportsMeetingMapper.batchEvent(list);
            }
        }
    }
}
